<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
        >添加客户账号</el-button>
      </el-col>
      <right-toolbar :search="false" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="accountList" border>
      <el-table-column label="客户名称" align="center" prop="userName" />
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="联系方式" align="center" prop="userPhone" />
      <el-table-column label="登录账号" align="center" prop="userPhone" />
      <!-- <el-table-column label="登录密码" align="center" prop="passWord" /> -->
      <el-table-column label="权限栏目" align="center" prop="menuId">
        <template slot-scope="scope">
          <dict-tag :options="accountOptions" :value="scope.row.menuId" />
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="创建人时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleResetPassword(scope.row)"
          >重置密码</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
           >修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改项目账号信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-radio-group v-model="form.sex">
                <el-radio label="男"></el-radio>
                <el-radio label="女"></el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="登录账号" prop="userPhone">
              <el-input v-model="form.userPhone" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职务" prop="position">
              <el-input v-model="form.position" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="权限栏目" props="menuIds">
              <el-checkbox-group v-model="menuIds">
                <el-checkbox v-for="item in accountOptions" :key="item.value" :label="item.value">{{ item.label
                  }}</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAccount, getAccount, delAccount, addAccount, updateAccount, resetPasswordAccount } from "@/api/cld/account";
import { accountOptions } from '@/assets/staticDict'

export default {
  name: "Account",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目账号信息表格数据
      accountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: this.$route.params.projectId,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userPhone: [
          { required: true, message: "登录账号不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],
        position: [
          { required: true, message: "职务不能为空", trigger: "blur" }
        ],
        menuIds: [
          { required: true, message: "请选择权限栏目", trigger: "blur" }
        ],
      },
      // 权限栏目
      accountOptions: accountOptions,
      // 项目id
      projectId: this.$route.params.projectId,
      menuIds: []
    };

  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询项目账号信息列表 */
    getList() {
      this.loading = true;
      listAccount(this.queryParams).then(response => {
        this.accountList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        projectAccountId: null,
        projectId: this.projectId,
        userPhone: null,
        passWord: 123456,
        customerId: null,
        createId: null,
        createName: null,
        createTime: null,
        flag: null,
        userName: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目账号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const projectAccountId = row.projectAccountId || this.ids
      getAccount(projectAccountId).then(response => {
        this.form = response.data;
        this.menuIds = this.form.menuId?.split(',');
        this.open = true;
        this.title = "修改项目账号";
      });
    },
    // 重置密码
    handleResetPassword(row) {
      this.$modal.confirm('是否重置密码为[123456]？').then(function () {
        return resetPasswordAccount(row.projectAccountId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => { });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.menuId = this.menuIds.join(',');
          if (this.form.projectAccountId != null) {
            updateAccount(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAccount(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const projectAccountIds = row.projectAccountId || this.ids;
      this.$modal.confirm('是否确认删除项目账号信息编号为"' + projectAccountIds + '"的数据项？').then(function () {
        return delAccount(projectAccountIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
  }
};
</script>
